package com.chaosj.skill.prometheus.properties

import java.util.function.Function
import java.util.function.Supplier

/**
 * @Description:
 * @author caoyangjie
 * @date 2020年01月2020/1/23日 12:06
 * @Copyright: © 赢时胜
 */
class PropertiesConfigAdapter<T> {

    private T properties

    /**
     * Create a new {@link PropertiesConfigAdapter} instance.
     * @param properties the source properties
     */
    PropertiesConfigAdapter(T properties) {
        this.properties = properties
    }

    /**
     * Get the value from the properties or use a fallback from the {@code defaults}.
     * @param getter the getter for the properties
     * @param fallback the fallback method, usually super interface method reference
     * @param <V> the value type
     * @return the property or fallback value
     */
    protected final <V> V get(Function<T, V> getter, Supplier<V> fallback) {
        V value = getter.apply(this.properties)
        return (value != null) ? value : fallback.get()
    }

}